import * as XLSX from "xlsx";

const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';

export default (data, fileName) => {
    if (!Array.isArray(data)) {
        data = [data];
    }

    const workBook = {
        Sheets: {},
        SheetNames: []
    };

    data.forEach(item => {
        const workSheet = XLSX.utils.aoa_to_sheet(item.data);
        workBook.Sheets[item.sheetName] = workSheet;
        workBook.SheetNames.push(item.sheetName);
    });

    const excelBuffer = XLSX.write(
        workBook,
        {
            bookType: 'xlsx',
            type: 'array'
        }
    );
    const fileData = new Blob(
        [excelBuffer],
        {
            type: fileType
        }
    );

    // 下载
    const aLink = document.createElement('a');
    const url = URL.createObjectURL(fileData);
    aLink.href = url;
    aLink.download = fileName || '统计数据';
    let event;
	if (window.MouseEvent) {
        event = new MouseEvent('click');
    }
	else {
		event = document.createEvent('MouseEvents');
		event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	}
    aLink.dispatchEvent(event);
};